package com.sunwayhorizo.health.hl7.rim;

import com.sunwayhorizo.health.hl7.common.*;
import com.sunwayhorizo.health.hl7.datatypes.*;
import com.sunwayhorizo.health.hl7.datatypes.common.*;
import com.sunwayhorizo.health.hl7.factories.*;
import com.sunwayhorizo.health.hl7.persistent.*;
import static com.sunwayhorizo.health.hl7.vocabulary.EntityCdSetCode.*;
import java.io.*;
import java.sql.Timestamp;
import java.util.*;
import javax.persistence.*;

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorValue("PLC")
public class Place extends Hl7Entity implements Serializable
{

    /*
     * public AD addr; public ED directionsText; public ST gpsText; public BL
     * mobileInd; public ED positionText;
     */

    /**
     * 
     */
    protected static final long serialVersionUID = 1L;

    // Place
    @Column(name = "PL_GPS_TXT")
    protected String plGpsTxt;

    @Column(name = "PL_MOBILE_FLAG")
    protected String plMobileFlag; // 是否可移动的

    @Column(name = "PL_LEVEL_CODE_ETS_ID")
    protected String plLevelCodeEtsId = LEVELCODE.toString();
    @Column(name = "PL_TYPE_CODE_ETS_ID")
    protected String plTypeCodeEtsId = TYPECODE.toString();


    // @Column(name = "PL_DIRECTIONS_TEXT_ED_ID")
    // protected Long plDirectionsTextEdId; //
    // 该属性可以包括寻找地址信息时，该网站是不够的方向，GPS信息不可用，和/或实体访问该网站不能利用GPS信息直接使用。它可能还包括信息有用的人来访的位置
    //
    // @Column(name = "PL_POSITION_TEXT_ED_ID")
    // protected Long plPositionTextEdId; // 映射方案

    public Place() throws Hl7Exception
    {
    }


    public Place(ReferenceModifier referencemodifier)
    {
        super(referencemodifier);
    }


    /**
     * 设置Place的类型
     * 
     * @param ce
     */
    public void setPlTypeCode(CE ce)
    {
        if (null != ce && ce.codeSystemEnName() != null && ce.codeName() != null)
        {
            setRimCoreEntyCdSets(ce, TYPECODE);
        }
        else
        {
            this.setPlTypeCodeEtsId(ce.code().toString());
        }
    }


    /**
     * 得到Place的类型（如床位类型）
     * 
     * @return
     * @throws Hl7Exception
     */
    public CE getPlTypeCode() throws Hl7Exception
    {
        if (this.getPlTypeCodeEtsId().equals(TYPECODE.toString()))
        {
            for (RimCoreEntyCdSets entyCdSets : this.getEntyCdSetsList())
            {
                if (this.getPlTypeCodeEtsId().equals(entyCdSets.getAttributeTypeCode()))
                {
                    return DataTypeUtil.getCE(entyCdSets);
                }
            }
        }
        else
            return DataTypeUtil.getCE(this.getPlTypeCodeEtsId());
        return null;
    }


    /**
     * 设置Place的等级
     * 
     * @param ce
     */
    public void setPlLevelCode(CE ce)
    {
        if (null != ce && ce.codeSystemEnName() != null && ce.codeName() != null)
        {
            setRimCoreEntyCdSets(ce, LEVELCODE);
        }
        else
        {
            this.setPlLevelCodeEtsId(ce.code().toString());
        }
    }


    /**
     * 得到Place的等级（如床位等级）
     * 
     * @return
     * @throws Hl7Exception
     */
    public CE getPlLevelCode() throws Hl7Exception
    {
        if (this.getPlLevelCodeEtsId().equals(LEVELCODE.toString()))
        {
            for (RimCoreEntyCdSets entyCdSets : this.getEntyCdSetsList())
            {
                if (this.getPlLevelCodeEtsId().equals(entyCdSets.getAttributeTypeCode()))
                {
                    return DataTypeUtil.getCE(entyCdSets);
                }
            }
        }
        else
            return DataTypeUtil.getCE(this.getPlLevelCodeEtsId());
        return null;
    }


    // 保存地址信息
    protected void newAddr(AD addr)
    {
        RimCoreEntyAddrs entyAddrs = new RimCoreEntyAddrs();
        entyAddrs.setFormatted(addr.formatted().toString());
        entyAddrs.setEntyAddrUses(addr.use().toSet());
        entyAddrs.setEntyAddrParts(((ADImpl) addr).toList());
        this.addEntyAddrs(entyAddrs);
    }


    /**
     * 设置Place的地址，保存和修改地址都用此方法
     * 
     * @param addr
     *            place地址信息
     */
    @SuppressWarnings("rawtypes")
    public void setAddr(AD addr)
    {
        List mList = addr.toList();
        List<RimCoreEntyAddrs> addrList = this.getEntyAddrsList();
        if (ListUtil.isNotEmpty(addrList))
        {
            for (int i = 0; i < addrList.size(); i++)
            {
                RimCoreEntyAddrs addrs = addrList.get(i);
                String addrFormat = "";
                List<RimCoreEntyAddrParts> addrParts = addrs.getEntyeAddrPartsList();
                if (ListUtil.isNotEmpty(addrParts))
                {
                    for (int j = 0; j < addrParts.size(); j++)
                    {
                        RimCoreEntyAddrParts addPart = addrParts.get(j);
                        for (int k = 0; k < mList.size(); k++)
                        {
                            ADXPImpl ad = (ADXPImpl) mList.get(k);
                            if (addPart.getPartType().equals(ad.partType().code().toString()))
                            {
                                addrFormat += ad.literal().toString();
                                addPart.setLiteral(ad.literal().toString());
                                addPart.setPartOrderNum((long) k);
                            }
                        }
                    }
                }
                addrs.setFormatted(addrFormat);
            }
        }
        else
        {
            newAddr(addr);
        }
    }


    /**
     * 得到Place的地址信息
     * 
     * @return AD
     * @throws Hl7Exception
     */
    @SuppressWarnings("rawtypes")
    public AD getAddr() throws Hl7Exception
    {
        if (ListUtil.isNotEmpty(getEntyAddrsList()))
        {
            RimCoreEntyAddrs entyAddrs = (RimCoreEntyAddrs) this.getEntyAddrsList().get(0);
            List mList = entyAddrs.getEntyeAddrPartsList();
            int size = mList.size();
            String value[] = new String[size];
            String type[] = new String[size];
            for (int i = 0; i < size; i++)
            {
                RimCoreEntyAddrParts partAddr = (RimCoreEntyAddrParts) mList.get(i);
                value[i] = partAddr.getLiteral();
                type[i] = partAddr.getPartType();
            }
            RimCoreEntyAddrUses addrUses = (RimCoreEntyAddrUses) entyAddrs.getEntyAddrUsesList().get(0);
            String useCode = addrUses.getUse();
            return DataTypeUtil.getAD(value, type, useCode);
        }
        return null;
    }


    public void setDirectionsText(ED directionsText)
    {
        // RimCoreEds ed = new RimCoreEds();
        RimCoreEds ed = this.getRimCoreEdsPlDirectionsTextId();
        ed.setEdClob(directionsText.literal().toString().toCharArray());
        ed.setMediaTypeCode(directionsText.mediaType().toString());
        ed.setCharacterSetCode(directionsText.charset().toString());
        ed.setLanguageCode(directionsText.language().toString());
        ed.setCompressionCode(directionsText.compression().toString());
        ed.setRefTelAddressTxt(directionsText.reference().address().toString());
        ed.setRefTelSchemeCode(directionsText.reference().scheme().toString());
        ed.setRefTelStartDateTs(new Timestamp(directionsText.reference().useablePeriod().hull().low().toDate().getTime()));
        ed.setRefTelEndDateTs(new Timestamp(directionsText.reference().useablePeriod().hull().high().toDate().getTime()));
        ed.setRefTelGtsTxt(directionsText.reference().useablePeriod().literal().toString());
        ed.setEdRefTelUses(directionsText.reference().use().toSet());
        ed.setIntegrityCheckAlgCode(directionsText.integrityCheckAlgorithm().toString());
        ed.setIntegrityCheckTxt(directionsText.integrityCheck().stringValue());
        // ed.addRimCoreEdsPlDirectionsTextId(this);
        this.setRimCoreEdsPlDirectionsTextId(ed);
    }


    public ED getDirectionsText() throws Hl7Exception
    {
        RimCoreEds ed = this.getRimCoreEdsPlDirectionsTextId();
        if (ed != null)
        {
            return DataTypeUtil.getED(String.valueOf(ed.getEdClob()), ed.getEdBlob());
        }
        else
        {
            return DataTypeFactory.getInstance().nullED(DataTypeUtil.getCS(""));
        }
    }


    public void setGpsText(ST gpsText)
    {
        this.setPlGpsTxt(gpsText.toString());
    }


    public ST getGpsText() throws Hl7Exception
    {
        return DataTypeUtil.getST(this.getPlGpsTxt());
    }


    public void setMobileInd(BL mobileInd)
    {
        this.setPlMobileFlag(mobileInd.literal().toString());
    }


    public BL getMobileInd() throws Hl7Exception
    {
        return DataTypeUtil.getBL(this.getPlMobileFlag());
    }


    public void setPositionText(ED positionText)
    {
        // RimCoreEds ed = new RimCoreEds();
        RimCoreEds ed = this.getRimCoreEdsPlPositionTextId();
        if (positionText.literal().toString() != null && !positionText.literal().toString().equals(""))
        {
            ed.setEdClob(positionText.literal().toString().toCharArray());
        }

        if (positionText.mediaType() != null && !positionText.mediaType().equals(""))
        {
            ed.setMediaTypeCode(positionText.mediaType().toString());
        }

        ed.setCharacterSetCode(positionText.charset().toString());
        ed.setLanguageCode(positionText.language().toString());
        ed.setCompressionCode(positionText.compression().toString());
        ed.setRefTelAddressTxt(positionText.reference().address().toString());
        ed.setRefTelSchemeCode(positionText.reference().scheme().toString());

        if (positionText.reference().useablePeriod().hull().low().toDate() != null
                && !positionText.reference().useablePeriod().hull().low().toDate().equals(""))
        {
            ed.setRefTelStartDateTs(new Timestamp(positionText.reference().useablePeriod().hull().low().toDate().getTime()));
        }

        if (positionText.reference().useablePeriod().hull().high().toDate() != null
                && !positionText.reference().useablePeriod().hull().high().toDate().equals(""))
        {
            ed.setRefTelEndDateTs(new Timestamp(positionText.reference().useablePeriod().hull().high().toDate().getTime()));
        }

        if (positionText.reference().useablePeriod().literal() != null && !positionText.reference().useablePeriod().literal().equals(""))
        {
            ed.setRefTelGtsTxt(positionText.reference().useablePeriod().literal().toString());
        }
        if (positionText.reference().use().toSet() != null && !positionText.reference().use().toSet().equals(""))
        {
            ed.setEdRefTelUses(positionText.reference().use().toSet());
        }
        ed.setIntegrityCheckAlgCode(positionText.integrityCheckAlgorithm().toString());
        ed.setIntegrityCheckTxt(positionText.integrityCheck().stringValue());
        // ed.addRimCoreEdsPlPositionTextId(this);
        this.setRimCoreEdsPlPositionTextId(ed);
    }


    public void updatePositionText(ED positionText)
    {

        RimCoreEds ed = getRimCoreEdsPlDirectionsTextId();
        if (positionText.literal() != null && !positionText.literal().equals(""))
        {
            ed.setEdClob(positionText.literal().toString().toCharArray());
        }

        if (positionText.mediaType() != null && !positionText.mediaType().equals(""))
        {
            ed.setMediaTypeCode(positionText.mediaType().toString());
        }

        ed.setCharacterSetCode(positionText.charset().toString());
        ed.setLanguageCode(positionText.language().toString());
        ed.setCompressionCode(positionText.compression().toString());
        ed.setRefTelAddressTxt(positionText.reference().address().toString());
        ed.setRefTelSchemeCode(positionText.reference().scheme().toString());

        if (positionText.reference().useablePeriod().hull().low().toDate() != null
                && !positionText.reference().useablePeriod().hull().low().toDate().equals(""))
        {
            ed.setRefTelStartDateTs(new Timestamp(positionText.reference().useablePeriod().hull().low().toDate().getTime()));
        }

        if (positionText.reference().useablePeriod().hull().high().toDate() != null
                && !positionText.reference().useablePeriod().hull().high().toDate().equals(""))
        {
            ed.setRefTelEndDateTs(new Timestamp(positionText.reference().useablePeriod().hull().high().toDate().getTime()));
        }

        if (positionText.reference().useablePeriod().literal() != null && !positionText.reference().useablePeriod().literal().equals(""))
        {
            ed.setRefTelGtsTxt(positionText.reference().useablePeriod().literal().toString());
        }
        if (positionText.reference().use().toSet() != null && !positionText.reference().use().toSet().equals(""))
        {
            ed.setEdRefTelUses(positionText.reference().use().toSet());
        }
        ed.setIntegrityCheckAlgCode(positionText.integrityCheckAlgorithm().toString());
        ed.setIntegrityCheckTxt(positionText.integrityCheck().stringValue());
        // ed.addRimCoreEdsPlPositionTextId(this);
        this.setRimCoreEdsPlPositionTextId(ed);
    }


    public ED getPositionText() throws Hl7Exception
    {
        RimCoreEds ed = getRimCoreEdsPlDirectionsTextId();
        if (ed != null)
        {
            return DataTypeUtil.getED(String.valueOf(ed.getEdClob()), ed.getEdBlob());
        }
        else
        {
            return DataTypeFactory.getInstance().nullED(DataTypeUtil.getCS(""));
        }
    }


    protected void setPlGpsTxt(String plGpsTxt)
    {
        this.plGpsTxt = plGpsTxt;
    }


    protected String getPlGpsTxt()
    {
        return plGpsTxt;
    }


    protected void setPlMobileFlag(String plMobileFlag)
    {
        this.plMobileFlag = plMobileFlag;
    }


    protected String getPlMobileFlag()
    {
        return plMobileFlag;
    }


    protected String getPlLevelCodeEtsId()
    {
        return plLevelCodeEtsId;
    }


    protected void setPlLevelCodeEtsId(String plLevelCodeEtsId)
    {
        this.plLevelCodeEtsId = plLevelCodeEtsId;
    }


    protected String getPlTypeCodeEtsId()
    {
        return plTypeCodeEtsId;
    }


    protected void setPlTypeCodeEtsId(String plTypeCodeEtsId)
    {
        this.plTypeCodeEtsId = plTypeCodeEtsId;
    }

}
